/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ses;

import Entity.Advertise;
import ses.exceptions.IllegalOrphanException;
import ses.exceptions.NonexistentEntityException;
import ses.exceptions.PreexistingEntityException;
import ses.exceptions.RollbackFailureException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import Entity.AdvertiseType;
import Entity.Accounts;
import java.util.ArrayList;
import javax.transaction.UserTransaction;

/**
 *
 * @author Huu Vinh
 */
public class AdvertiseJpaController implements Serializable {

    public AdvertiseJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Advertise advertise) throws IllegalOrphanException, PreexistingEntityException, RollbackFailureException, Exception {
        List<String> illegalOrphanMessages = null;
        Accounts accountsOrphanCheck = advertise.getAccounts();
        if (accountsOrphanCheck != null) {
            Advertise oldAdvertiseOfAccounts = accountsOrphanCheck.getAdvertise();
            if (oldAdvertiseOfAccounts != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("The Accounts " + accountsOrphanCheck + " already has an item of type Advertise whose accounts column cannot be null. Please make another selection for the accounts field.");
            }
        }
        if (illegalOrphanMessages != null) {
            throw new IllegalOrphanException(illegalOrphanMessages);
        }
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            AdvertiseType advertiseTypeId = advertise.getAdvertiseTypeId();
            if (advertiseTypeId != null) {
                advertiseTypeId = em.getReference(advertiseTypeId.getClass(), advertiseTypeId.getAdvertiseTypeId());
                advertise.setAdvertiseTypeId(advertiseTypeId);
            }
            Accounts accounts = advertise.getAccounts();
            if (accounts != null) {
                accounts = em.getReference(accounts.getClass(), accounts.getAccId());
                advertise.setAccounts(accounts);
            }
            em.persist(advertise);
            if (advertiseTypeId != null) {
                advertiseTypeId.getAdvertiseList().add(advertise);
                advertiseTypeId = em.merge(advertiseTypeId);
            }
            if (accounts != null) {
                accounts.setAdvertise(advertise);
                accounts = em.merge(accounts);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            if (findAdvertise(advertise.getAdvertiseId()) != null) {
                throw new PreexistingEntityException("Advertise " + advertise + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Advertise advertise) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Advertise persistentAdvertise = em.find(Advertise.class, advertise.getAdvertiseId());
            AdvertiseType advertiseTypeIdOld = persistentAdvertise.getAdvertiseTypeId();
            AdvertiseType advertiseTypeIdNew = advertise.getAdvertiseTypeId();
            Accounts accountsOld = persistentAdvertise.getAccounts();
            Accounts accountsNew = advertise.getAccounts();
            List<String> illegalOrphanMessages = null;
            if (accountsNew != null && !accountsNew.equals(accountsOld)) {
                Advertise oldAdvertiseOfAccounts = accountsNew.getAdvertise();
                if (oldAdvertiseOfAccounts != null) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("The Accounts " + accountsNew + " already has an item of type Advertise whose accounts column cannot be null. Please make another selection for the accounts field.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (advertiseTypeIdNew != null) {
                advertiseTypeIdNew = em.getReference(advertiseTypeIdNew.getClass(), advertiseTypeIdNew.getAdvertiseTypeId());
                advertise.setAdvertiseTypeId(advertiseTypeIdNew);
            }
            if (accountsNew != null) {
                accountsNew = em.getReference(accountsNew.getClass(), accountsNew.getAccId());
                advertise.setAccounts(accountsNew);
            }
            advertise = em.merge(advertise);
            if (advertiseTypeIdOld != null && !advertiseTypeIdOld.equals(advertiseTypeIdNew)) {
                advertiseTypeIdOld.getAdvertiseList().remove(advertise);
                advertiseTypeIdOld = em.merge(advertiseTypeIdOld);
            }
            if (advertiseTypeIdNew != null && !advertiseTypeIdNew.equals(advertiseTypeIdOld)) {
                advertiseTypeIdNew.getAdvertiseList().add(advertise);
                advertiseTypeIdNew = em.merge(advertiseTypeIdNew);
            }
            if (accountsOld != null && !accountsOld.equals(accountsNew)) {
                accountsOld.setAdvertise(null);
                accountsOld = em.merge(accountsOld);
            }
            if (accountsNew != null && !accountsNew.equals(accountsOld)) {
                accountsNew.setAdvertise(advertise);
                accountsNew = em.merge(accountsNew);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = advertise.getAdvertiseId();
                if (findAdvertise(id) == null) {
                    throw new NonexistentEntityException("The advertise with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Advertise advertise;
            try {
                advertise = em.getReference(Advertise.class, id);
                advertise.getAdvertiseId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The advertise with id " + id + " no longer exists.", enfe);
            }
            AdvertiseType advertiseTypeId = advertise.getAdvertiseTypeId();
            if (advertiseTypeId != null) {
                advertiseTypeId.getAdvertiseList().remove(advertise);
                advertiseTypeId = em.merge(advertiseTypeId);
            }
            Accounts accounts = advertise.getAccounts();
            if (accounts != null) {
                accounts.setAdvertise(null);
                accounts = em.merge(accounts);
            }
            em.remove(advertise);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Advertise> findAdvertiseEntities() {
        return findAdvertiseEntities(true, -1, -1);
    }

    public List<Advertise> findAdvertiseEntities(int maxResults, int firstResult) {
        return findAdvertiseEntities(false, maxResults, firstResult);
    }

    private List<Advertise> findAdvertiseEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Advertise as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Advertise findAdvertise(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Advertise.class, id);
        } finally {
            em.close();
        }
    }

    public int getAdvertiseCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Advertise as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
